package a.student.dao;

import java.util.ArrayList;

import a.student.library.*;
import a.student.library.Class;
import a.student.helper.DatabaseHelper;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class ClassDAO extends BaseDao<Class>{

	public ClassDAO(Context context, String databaseName) {
		super(context, databaseName);
	}
	
	public ClassDAO(SQLiteDatabase db) {
		super(db);
	}
	
	public ClassDAO(DatabaseHelper helper) {
		super(helper);
	}
	
	public long addNewClass(Class classInfo){
		
		ContentValues cv = new ContentValues();
		cv.put(Class.CLASS_NAME, classInfo.className);
		cv.put(Class.CLASS_TYPE, classInfo.type);
		cv.put(Class.CLASS_CID, classInfo.courseID);
		cv.put(Class.CLASS_CTID, classInfo.instructorID);
		cv.put(Class.CLASS_LOCATION, classInfo.location);
		cv.put(Class.CLASS_DATESTART, classInfo.dateStart);
		cv.put(Class.CLASS_DATEEND, classInfo.dateEnd);
		cv.put(Class.CLASS_TIMESTART, classInfo.timeStart);
		cv.put(Class.CLASS_TIMEEND, classInfo.timeEnd);
		cv.put(Class.CLASS_REPEAT, classInfo.repeat);
		cv.put(Class.CLASS_DATEREPEAT, classInfo.dateRepeat);
		
		long result = super.insert(Class.CLASS_TBLNAME, cv);
		if(result > 0)
			classInfo.setClassId((int)result);
		return result;
		
	}

	public long removeClass(Class classInfo){
		
		ContentValues cv = new ContentValues();
		cv.put(Assignment.ASSIGNMENT_ID, classInfo.getClassId());
		long count = super.delete(Class.CLASS_TBLNAME, cv);
		return count;
		
	}
    
    public ArrayList<Class> getAllClass(){
    	Cursor c = super.getAll(Class.CLASS_TBLNAME, Class.CLASS_DEFAULT_SORT_ORDER);
    	ArrayList<Class> array = new ArrayList<Class>();
    	if(c==null)
    		return array;
    	while(!c.isAfterLast()){
    		array.add(new Class(c));
    		c.moveToNext();
    	}
    	c.close();
    	return array;
    }
    
    
    
    public Class findClass(final int classID) {
    	
        ContentValues cv = new ContentValues();
        cv.put(Class.CLASS_ID, classID);
        Cursor c = super.find(Class.CLASS_TBLNAME, cv,Class.CLASS_DEFAULT_SORT_ORDER);
        Class classInfo = null;
        if (c != null) {
            try {
            	classInfo = new Class(c);
            } catch (Exception e) {
            	e.printStackTrace();
			}
            finally {
                c.close();
            }
        }
        return classInfo;
    }
    
	public boolean updateClassDetail(Class classInfo){
		
		boolean flag = true;
		ContentValues cv = new ContentValues();
		cv.put(Class.CLASS_NAME, classInfo.className);
		cv.put(Class.CLASS_TYPE, classInfo.type);
		cv.put(Class.CLASS_CID, classInfo.courseID);
		cv.put(Class.CLASS_CTID, classInfo.instructorID);
		cv.put(Class.CLASS_LOCATION, classInfo.location);
		cv.put(Class.CLASS_DATESTART, classInfo.dateStart);
		cv.put(Class.CLASS_DATEEND, classInfo.dateEnd);
		cv.put(Class.CLASS_TIMESTART, classInfo.timeStart);
		cv.put(Class.CLASS_TIMEEND, classInfo.timeEnd);
		cv.put(Class.CLASS_REPEAT, classInfo.repeat);
		cv.put(Class.CLASS_DATEREPEAT, classInfo.dateRepeat);
		flag = super.update(Class.CLASS_TBLNAME, cv, Class.CLASS_ID + "=?", new String[]{String.valueOf(classInfo.getClassId())})>0;
    	return flag;
    	
	}


}
